<?
require_once("lib/php/model/DataInterface.php");
require_once("lib/php/Singleton.php");

class MySqlInterface extends Singleton implements DataInterface {
	public static function getInstance() {
		return parent::getInstance(__CLASS__);
	}

	public function connect($host, $user, $password, $name) {
		static $connected = false;
		
		if (!$connected) {
			mysql_connect($host, $user, $password) or die("MySqlInterface.connect(): Can't connect to database.");
			mysql_select_db($name);
			$connected = true;
		}
	}

	public function executeQuery($query) {
		if ($result = mysql_query($query)) {
			return $result;
		} else {
			return null;
		}
	}
	
	public function executeNonQuery($query) {
		if (mysql_query($query)) {
			return mysql_insert_id();
		} else {
			return null;
		}
	}
	
	public function executeScalar($query) {
		if ($result = mysql_query($query)) {
			$row = mysql_fetch_row($result);
			return $row[0];
		} else {
			return null;
		}
	}
	
	public function getNextRow($result) {
		return mysql_fetch_assoc($result);
	}
	
	public function getError() {
		return mysql_error();
	}
	
	public function sanitize($str) {
        return mysql_real_escape_string($str);
    }
}
?>
